﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Timer=System.Threading.Timer;

namespace SearchRS
{
    public abstract class DMLinks
    {
        protected string m_dmPath;
        protected Timer m_timer;

        public DMLinks(string dmPath)
        {
            m_dmPath = dmPath;
        }

        public bool ValidateDmPath()
        {
            if (!String.IsNullOrEmpty(m_dmPath))
            {
                if (!File.Exists(m_dmPath))
                {
                    MessageBox.Show("Download Manager not found in " + m_dmPath + ". Please configure the right path in config file.");
                    return false;
                }
            }

            return true;
        }

        public List<string> RsLinks { get; set; }
        public string SubsLink { get; set; }
        public string Passowrd { get; set; }

        public abstract bool AddLinksAndPasswordToDM();
    }

    public class IDMLinks : DMLinks
    {
        private string m_dmParams = "/a /n /d";

        public IDMLinks(string dmPath) : base(dmPath)
        {
        }

        public override bool AddLinksAndPasswordToDM()
        {
            if (!ValidateDmPath())
                return false;

            foreach (string rsLink in RsLinks)
            {
                Utils.Utils.RunShell(m_dmPath, m_dmParams + " " + rsLink);
            }

/*            if (!string.IsNullOrEmpty(SubsLink))
                Utils.Utils.RunShell(m_dmPath, m_dmParams + " " + SubsLink);
            */
            return true;
        }
    }

    public class FDMLinks : DMLinks
    {
        private string m_dmParams = "-fs";
        public FDMLinks(string dmPath) : base(dmPath)
        {
        }

        public override bool AddLinksAndPasswordToDM()
        {
            if (!ValidateDmPath())
                return false;

            foreach (string rsLink in RsLinks)
            {
                Utils.Utils.RunShell(m_dmPath, m_dmParams + " " + rsLink);
            }

/*            if (!string.IsNullOrEmpty(SubsLink))
                Utils.Utils.RunShell(m_dmPath, m_dmParams + " " + SubsLink);
            */
            return true;
        }
    }

    public class JDLinks : DMLinks
    {
        private string m_dmParams;

        public JDLinks(string dmPath) : base(dmPath)
        {
        }

        public override bool AddLinksAndPasswordToDM()
        {
            m_dmParams = "--hide --add-link";

            if (!ValidateDmPath())
                return false;

            string s = "";
            foreach (string link in RsLinks)
            {
                s += link + " ";
            }
            /*
            if (!string.IsNullOrEmpty(SubsLink))
            {
                s += SubsLink;
            }*/

            m_dmParams += " " + s;
            File.AppendAllText("RapidSearcher.log", "Running: " + m_dmPath + " " + m_dmParams + "\n");
            Utils.Utils.RunShell(m_dmPath, m_dmParams);
            Thread.Sleep(10000);

            if (!string.IsNullOrEmpty(Passowrd))
            {
                m_dmParams = "--add-password " + Passowrd;
                File.AppendAllText("RapidSearcher.log", "Running " + m_dmPath + " " + m_dmParams + "\n");
                Utils.Utils.RunShell(m_dmPath, m_dmParams);
            }

            m_timer = new Timer(
                delegate
                {
                    m_dmParams = "--start-download";
                    File.AppendAllText("RapidSearcher.log", "Running " + m_dmPath + " " + m_dmParams + "\n");
                    Utils.Utils.RunShell(m_dmPath, m_dmParams);                                           
                },null, 120 * 1000,Timeout.Infinite);
            return true;
        }
    }

}
